package com.zy.design.pattern.proxy.rpc.client;

import com.zy.design.pattern.proxy.rpc.RPCMeat;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author zhaoyi
 * @date 2021/4/8 17:29
 */
public class BeanProxy {

    private static ClientInstance client = new ClientInstance("127.0.0.1", 8080);

    //获取代理对象
    public static Object getProxyBean(Class clazz) {
        return Proxy.newProxyInstance(
                clazz.getClassLoader(),
                new Class[] {clazz},//接口这样穿 不能clazz.getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        RPCMeat rpcMeat = new RPCMeat(method.getName(), args,clazz);
                        return client.sendRPC(rpcMeat);
                    }
                });

    }
}
